package com.xbongbong.paas.model.impl;

import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.model.CloudCodeControlModel;
import com.xbongbong.pro.domain.dao.CloudCodeControlDao;
import com.xbongbong.pro.domain.entity.CloudCodeControlEntity;
import com.xbongbong.saas.constant.BasicConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * paasAppModel业务实现
 * @author kaka
 * @time 2018-7-30 18:07
 */
@Service("cloudCodeControlModel")
public class CloudCodeControlModelImpl implements CloudCodeControlModel {
    private static final Logger LOG = LoggerFactory.getLogger(CloudCodeControlModelImpl.class);
    @Resource
    private CloudCodeControlDao cloudCodeControlDao;

    @Override
    public Long insert(CloudCodeControlEntity entity) {
        return cloudCodeControlDao.insert(entity);
    }

    @Override
    public void insertBatch(List<CloudCodeControlEntity> list) {
        cloudCodeControlDao.insertBatch(list);
    }

    @Override
    public CloudCodeControlEntity getByKey(Long key, String corpid) {
        return cloudCodeControlDao.getByKey(key, corpid);
    }

    @Override
    public CloudCodeControlEntity getByCcId(Long ccId, String corpid) {
        return cloudCodeControlDao.getByCcId(ccId, corpid);
    }

    @Override
    public Long update(CloudCodeControlEntity entity) {
        return cloudCodeControlDao.update(entity);
    }

    @Override
    public List<CloudCodeControlEntity> findEntities(Map<String, Object> param) {
        return cloudCodeControlDao.findEntities(param);
    }

    @Override
    public List<CloudCodeControlEntity> getByType(String corpid, PlatFormEnum platFormEnum, Long appId, Long formId, Integer businessType, Integer type) {
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        if (Objects.nonNull(businessType) && !Objects.equals(businessType, 0)) {
            param.put("businessType", businessType);
        } else if (Objects.nonNull(appId) && Objects.nonNull(formId) &&
                !Objects.equals(appId, 0L) && !Objects.equals(formId, 0L)) {
            param.put("appId", appId);
            param.put("formId", formId);
        } else {
            return new ArrayList<>();
        }

        if (Objects.nonNull(platFormEnum)) {
            List<String> platformIn = new ArrayList<>();
            platformIn.add(PlatFormEnum.ALL.getValue());
            platformIn.add(platFormEnum.getValue());
            param.put("platformIn", platformIn);
        }

        param.put("type", type);
        param.put("enable", BasicConstant.ONE);
        param.put("del", BasicConstant.ZERO);
        return findEntities(param);
    }

    @Override
    public Integer getEntitiesCount(Map<String, Object> param) {
        return cloudCodeControlDao.getEntitiesCount(param);
    }

    @Override
    public Long delete(Long key, String corpid) {
        return cloudCodeControlDao.deleteByKey(key, corpid);
    }

    @Override
    public Long deleteByCcId(String corpid, Long ccId) {
        return cloudCodeControlDao.deleteByCcId(corpid, ccId);
    }
}
